package com.dk.controller;


import com.alibaba.fastjson.JSONObject;
import com.dk.data.Result;
import com.dk.domain.TCarSeriesDTO;
import com.dk.em.ConstantEnum;
import com.dk.pojo.TCar;
import com.dk.pojo.TCarSeries;
import com.dk.service.ITCarSeriesService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 车系表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/t-car-series")
public class TCarSeriesController {

    @Autowired
    private ITCarSeriesService itCarSeriesService;

    @ApiOperation(value = "车系查询")
    @GetMapping
    public Result getInfoIdBy(){
        List<TCarSeries> list = itCarSeriesService.getInfoAll();
        //数据传输转换
        List<TCarSeriesDTO> dtoList = JSONObject.parseArray(JSONObject.toJSONString(list), TCarSeriesDTO.class);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(dtoList);
    }

    @ApiOperation(value = "车系增加操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "车系名称",required = true,paramType = "query"),
            @ApiImplicitParam(name = "brandId",value = "品牌分类",required = true,paramType = "query"),
    })
    @PostMapping("/add")
    public Result add(TCarSeries tCarSeries){
        tCarSeries.setDeleted(0);
        tCarSeries.setVersion(1);
        tCarSeries.setCreateTime(new Date());
        tCarSeries.setUpdateTime(new Date());
        boolean b = itCarSeriesService.save(tCarSeries);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS);
        }
        return Result.bulid(ConstantEnum.ERROR);
    }

    @ApiOperation(value = "车系信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "TCarSeries",value = "实体类:车系",required = true,paramType = "body",dataTypeClass = TCar.class)
    })
    @PutMapping(value = "/update")
    public Result getInfoIdup(@RequestBody TCarSeries tCarSeries){

        Integer i = itCarSeriesService.updateInfo(tCarSeries);
        if(i>0){
            return Result.bulid(ConstantEnum.SUCCESS);
        }
        return Result.bulid(ConstantEnum.ERROR);
    }

    @ApiOperation(value = "逻辑删除")
    @ApiImplicitParam(name = "id",value = "id",required = true,allowMultiple = true,dataType = "int")
    @DeleteMapping("/del")
    public Result del(@RequestParam("id") Integer id){
        //删除
        Integer integer = itCarSeriesService.deleById(id);
        if(integer > 0){
            return Result.bulid(ConstantEnum.SUCCESS);
        }
        return Result.bulid(ConstantEnum.ERROR);
    }
}
